
//
//  LoginViewController.m
//  JxCarios
//
//  Created by xcq on 15/1/27.
//  Copyright (c) 2015年 xiongchuanqi. All rights reserved.
//

#import "LoginViewController.h"
#import "CurrentDataSource.h"
#import "RegisterViewController.h"

@interface LoginViewController (){
    CurrentDataSource *dataSource;
}
@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"登录"];
    [self initAbout];
}

- (void)initAbout{
    _registerBtn.layer.borderColor = BTN_COLOR.CGColor;
    _registerBtn.layer.cornerRadius = 3.0f;
    _registerBtn.layer.borderWidth = 1.0f;
    [self showNavBarWithRightTitle:@"找回密码"
                    andRightAction:@selector(pushToForgetPassWord)
                     andLeftAction:@selector(dismiss)
                       andFontSize:13.0
                     andRightWidth:65];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(autoLoginAction:)];
    [_autoLoginView addGestureRecognizer:tap];
    _LoginView.layer.borderColor = UICOLOR_MAKE_RGB(239, 238, 244).CGColor ;
    _LoginView.layer.borderWidth = 0.8f;
}

- (void)dismiss {
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

- (void)autoLoginAction:(UIGestureRecognizer *)sender{
    _autoLoginBtn.selected = !_autoLoginBtn.selected;
    
}

- (void)pushToForgetPassWord{
    [self pushToRegisterOrGetPsd:2];
}

#pragma mark - UITextField delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    return YES;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string]; //得到输入框的内容
    if (textField.tag == 200) { //账号输入
        //只能输入数字
        NSCharacterSet *cs;
        cs = [[NSCharacterSet characterSetWithCharactersInString:Point]invertedSet];
        NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""]; //按cs分离出数组,数组按@""分离出字符串
        BOOL canChange = [string isEqualToString:filtered];
        return canChange;
    }else if (textField.tag == 201){ //密码输入
        if(toBeString.length <= 15){
            return YES;
        }else {
            return NO;
        }
    }
    
    return YES;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
 

- (IBAction)login:(id)sender {
    
    [self.view endEditing:YES];
    if(_userName.text.length <= 0){
        [self.navigationController.view makeToast:@"请输入用户名"];
        return;
    }
    
    
    if (_passWord.text.length <= 0) {
        [self.navigationController.view makeToast:@"请输入密码"];
        return;
    }
    
    if (!dataSource) {
        dataSource = [CurrentDataSource dataSourceWithRequestor:self];
    }
    __weak LoginViewController *self_temp = self;
    dataSource.passWord = _passWord.text;
    dataSource.userName = _userName.text;
    [dataSource userLogin:^(id result) {
        NSLog(@"%@",result);
        [Common setLoginStatus:YES];
        [Common setUserInfo:[result customObjectForkey:@"memberinfo"]];
        if (_autoLoginBtn.selected) {
            [Common setUserName:_userName.text];
            [Common setPassWord:_passWord.text];
            [Common setAutoLogin:_autoLoginBtn.selected];
        }
        [self dismiss];
    }andFail:^(id result) {
        
    }];
    
}

- (void)pushToRegisterOrGetPsd:(NSInteger)status{
    RegisterViewController *regis = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"registerView"];
    [regis statusChange:status];
    [self.navigationController pushViewController:regis animated:YES];
    
}

- (IBAction)registerUser:(id)sender {
    
    [self pushToRegisterOrGetPsd:1];
}
@end
